using System;
using System.Collections.Generic;
using System.Windows.Forms;
using EnvDTE;

namespace SPDevExplorer
{
    public partial class SiteExplorer : UserControl
    {
        private static ServiceManager _serviceManager;
        private readonly Dictionary<string, DocumentEvents> _docEvents;
        private readonly Dictionary<string, TreeNode> _docNodes;
        private readonly List<TreeNode> _nodesLoading;
        private readonly Timer _timer = new Timer();
        private readonly Dictionary<string, Window> _windows;
        private DragHandler _dragHandler;

        public SiteExplorer()
        {
            try
            {
                InitializeComponent();
                AttachEventHandlersToTree();

                _serviceManager = new ServiceManager();
                _dragHandler = new DragHandler(_ctlExplorerTree, _serviceManager);


                _docEvents = new Dictionary<string, DocumentEvents>();
                _docNodes = new Dictionary<string, TreeNode>();
                _windows = new Dictionary<string, Window>();

                ctxMenuFile.Opening += ctxMenuFile_Opening;

                _timer.Interval = 90;
                _timer.Tick += ListFoldersForNode_Tick;

                _nodesLoading = new List<TreeNode>();
            }
            catch (Exception ex)
            {
                LogEvent(ex, false);
            }
        }

        // SvdO: for recording parent node and node text to make selected visible node after refresh, and action to be executed on the node (tree reload)
        public TreeNode ParentNodeAfterRefresh { get; set; }
        private string TextOfNodeToMakeCurrentAfterRefresh { get; set; }
        private Action<TreeNode> ActionOnNodeAfterRefresh {get; set; }

        // SvdO: flag to indicate if restricted set of files and folder or all files and folder must be displayed.
        //       Default we only display the restricted set
        private bool DisplayAllFoldersAndFilesInTree { get; set; }

        protected static SPDevExplorerWebService.DevServices DevServices(TreeNode oNode)
        {
            return _serviceManager.GetService(oNode);
        }

        private void SiteExplorer_Load(object sender, EventArgs e)
        {
            btnCommandPrompt.Visible = ExplorerSettings.WssInstalled;
        }
    }
}